function showPic(whichPic) {
	if (!document.getElementById('placeHolder')) {
		return true;
	}
	var source = whichPic.getAttribute('href');
	var image = document.getElementById('placeHolder');
	image.setAttribute('src', source);

	if (!document.getElementById('description')) {
		return false;
	}
	var text = whichPic.getAttribute('title');
	if (text == null) {
		text = '';
	}

	var msg = document.getElementById('description');
	msg.firstChild.nodeValue = text;

	return false;
}

function prepareGallery() {
	if (!document.getElementById) {
		return false;
	}

	if (!document.getElementsByTagName) {
		return false;
	}

	if  (!document.getElementById('imageGallery')) {
		return false;
	}

	var gallery = document.getElementById('imageGallery');
	var links = gallery.getElementsByTagName('a');
	for (var i = 0; i < links.length; ++i) {
		links[i].onclick = function() {
			return showPic(this);
		}
	}
}

function insertAfter(newElement, targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement, targetElement.nextSibling);
	}
}

window.onload = function() {
	if (!document.createElement) {
		return false;
	}

	if (!document.createTextNode) {
		return false;
	}

	if (!document.getElementById) {
		return false;
	}

	if (!document.getElementById('imageGallery')) {
		return false;
	}

	var placeHolder = document.createElement('img');
	placeHolder.setAttribute('id', 'placeHolder');
	placeHolder.setAttribute('src', 'd.png');
	placeHolder.setAttribute('alt', 'default image');

	var description = document.createElement('p');
	description.setAttribute('id', 'description');

	var descText = document.createTextNode('Choose an image');
	description.appendChild(descText);

	var gallery = document.getElementById('imageGallery');
	insertAfter(placeHolder, gallery);
	insertAfter(description, placeHolder);

	prepareGallery();
}
